//
//  RestaurantViewModel.swift
//  FeastBooking
//
//  Created by Imon on 2017/10/2.
//  Copyright © 2017年 Imon. All rights reserved.
//

import Foundation
import RxSwift
import Moya

class RestaurantViewModel {
    
    public static let share = RestaurantViewModel()
    
    private var provider: RxMoyaProvider<RestaurantApi>
    
    private init() {
        let endpointClosure = { (target: RestaurantApi) -> Endpoint<RestaurantApi> in
            let defaultEndpoint = RxMoyaProvider.defaultEndpointMapping(for: target)
            return defaultEndpoint.adding(newHTTPHeaderFields: ["content-type": "application/json"])
        }
        provider = RxMoyaProvider<RestaurantApi>(endpointClosure: endpointClosure)
    }
    
    func getRestaurants(page: Int, sort: String, minRating: String, minPrice: String, maxPrice: String, name: String) -> Observable<[RestaurantSummaryResponse]> {
        return provider.request(.getRestaurants(page: page, sort: sort, minRating: minRating, minPrice: minPrice, maxPrice: maxPrice, name: name))
               .mapJSON()
               .mapArray(type: RestaurantSummaryResponse.self)
    }
    
    func getRestaurant(id: Int) -> Observable<RestaurantDetailResponse> {
        return provider.request(.getRestaurant(id: id))
               .mapJSON()
               .mapObject(type: RestaurantDetailResponse.self)
    }
    
    func getRestaurantRating(id: Int) -> Observable<Any> {
        return provider.request(.getRestaurantRating(id: id))
               .mapJSON()
    }
    
    func postRestaurantRating(id: Int, rating: Int) -> Observable<Any> {
        return provider.request(.rateRestaurant(id: id, score: rating))
               .mapJSON()
    }
    
    func getRestaurantDiscount(hotelId: Int, since: String) -> Observable<[RestaurantDiscount]> {
        return provider.request(.getDiscounts(hotelId: hotelId, since: since))
            .mapJSON()
            .mapArray(type: RestaurantDiscount.self)
    }
    
    func getBanquetHall(id: Int) -> Observable<BanquetHallDetail> {
        return provider.request(.getBanquetHall(id: id))
               .mapJSON()
               .mapObject(type: BanquetHallDetail.self)
    }
    
    func getFeast(id: Int) -> Observable<FeastDetail> {
        return provider.request(.getFeast(id: id))
               .mapJSON()
               .mapObject(type: FeastDetail.self)
    }
    
    func getReview(id: Int) -> Observable<RestaurantReviewResponse> {
        return provider.request(.getReviews(id: id))
               .mapJSON()
               .mapObject(type: RestaurantReviewResponse.self)
    }
}
